package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainbean.module.ProduceProductIn;
import com.tyqx.hashchainbiz.service.ProduceProductInService;
import com.tyqx.hashchaindao.mapper.HashIndexMapper;
import com.tyqx.hashchaindao.mapper.ProduceProductInMapper;
import com.tyqx.hashchainutil.common.HashValueUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("produceProductInService")
public class ProduceProductInServiceImpl implements ProduceProductInService {

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private ProduceProductInMapper produceProductInMapper;

    @Async
    public Integer produceProductIn(ProduceProductIn produceProductIn) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        produceProductIn.setCreateTime(createTime);
        produceProductIn.setUpdateTime(createTime);
        String produceProductInStr= produceProductIn.toString();
        HashIndex hashIndex= HashValueUtil.getHashValue(produceProductInStr);
        Integer flag=hashIndexMapper.insert(hashIndex);

        produceProductIn.setHashValue(hashIndex.getHashValue());

        return produceProductInMapper.addProduceProductIn(produceProductIn);
    }

    @Override
    public List<ProduceProductIn> getProduceProductInById(String donateId, String userName) {
        return produceProductInMapper.getProduceProductInById(donateId);
    }

    @Override
    public List<ProduceProductIn> getProduceProductInByTime(Date startTime, Date endTime, String userName) {
        return produceProductInMapper.getProduceProductInByTime(startTime, endTime, userName);
    }

}
